﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;

namespace ManagementSystem
{
    /// <summary>
    /// Õpilase registreerimise aken
    /// </summary>
    public partial class _6pilaseRegistreerimine : Window
    {
        User tempUser = null;
        string aine;
        private StatusBar status;

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="status">Peaakna statusbar</param>
        /// <param name="user">Kasutaja, kes on sisse loginud</param>
        /// <param name="aine">Aine nimi, kuhu õpilane registreeritakse.</param>
        public _6pilaseRegistreerimine(StatusBar status, User user, string aine)
        {
            tempUser = user;
            this.aine = aine;
            this.status = status;
            InitializeComponent();
        }

        /// <summary>
        /// Kui vajutatakse nupule "registreeri", käivitub antud meetod.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        private void registreeributton_Click(object sender, RoutedEventArgs e)
        {
            clearLastBorderBrush();

            _6pilane student = new _6pilane();
            if (eesnimiBox.Text.Equals(""))
            {
                eesnimiBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                student.FirstName = eesnimiBox.Text;
            }

            if (perenimiBox.Text.Equals(""))
            {
                perenimiBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                student.LastName = perenimiBox.Text;
            }

            if (erialaBox.Text.Equals(""))
            {
                erialaBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                student.Speciality = erialaBox.Text;
            }

            if (gruppBox.Text.Equals(""))
            {
                gruppBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                student.StudentGroup = gruppBox.Text;
            }

            if (koodBox.Text.Equals(""))
            {
                koodBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            else
            {
                student.IdentityCode = koodBox.Text;
            }

            if (kommentaarBox.Text.Equals(""))
            {
                student.Comment = "";
            }
            else
            {
                student.Comment = kommentaarBox.Text;
            }

            student.User_ID = tempUser.User_ID;

            Thread workerThread = new Thread(() => this.saveData(student));
            workerThread.Start();
            this.Close();
        }

        /// <summary>
        /// Meetod uue õpilase salvestamiseks andmebaasi.
        /// </summary>
        /// <param name="user">Kasutaja, kelle õpilaste hulka uus õpilane lisatakse.</param>
        private void saveData(_6pilane user)
        {
            try
            {

                DataClasses1DataContext kont = new DataClasses1DataContext();
                Student newStudent = new Student();
                newStudent.firstName = user.FirstName;
                newStudent.lastName = user.LastName;
                newStudent.speciality = user.Speciality;
                newStudent.studentGroup = user.StudentGroup;
                newStudent.identityCode = user.IdentityCode;
                newStudent.comment = user.Comment;
                newStudent.user_ID = user.User_ID;
                kont.Students.InsertOnSubmit(newStudent);
                kont.SubmitChanges();

                Student_In_Subject stInS = new Student_In_Subject();
                Student stud = kont.Students.Single(s => s.firstName == user.FirstName &&
                    s.lastName == user.LastName);
                stInS.Student_ID = stud.Student_ID;
                Subject sub = kont.Subjects.Single(s => s.Name == aine);
                stInS.Subject_ID = sub.Subject_ID;
                stInS.Comment = "";
                stInS.User_ID = user.User_ID;
                kont.Student_In_Subjects.InsertOnSubmit(stInS);
                kont.SubmitChanges();

                Hinne h = new Hinne();
                h.Eesnimi = user.FirstName;
                h.Perenimi = user.LastName;
                h.Aine = aine;
                h.Hinne1 = 0;
                h.Hinne2 = 0;
                h.Hinne3 = 0;
                h.Hinne4 = 0;
                h.Hinne5 = 0;
                h.Hinne6 = 0;
                h.Hinne7 = 0;
                h.Hinne8 = 0;
                h.Hinne9 = 0;
                h.Hinne10 = 0;
                kont.Hinnes.InsertOnSubmit(h);
                kont.SubmitChanges();
                
                kont.Dispose();


                Action action = new Action(() => { status.Items.Clear(); status.Items.Add("Uus õpilane lisatud !"); });
                this.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
        }

        /// <summary>
        /// Taasta tagasiside andmiseks muudetud äärised.
        /// </summary>
        private void clearLastBorderBrush()
        {
            eesnimiBox.BorderBrush = new SolidColorBrush(Colors.Black);
            perenimiBox.BorderBrush = new SolidColorBrush(Colors.Black);
            erialaBox.BorderBrush = new SolidColorBrush(Colors.Black);
            gruppBox.BorderBrush = new SolidColorBrush(Colors.Black);
            koodBox.BorderBrush = new SolidColorBrush(Colors.Black);
            kommentaarBox.BorderBrush = new SolidColorBrush(Colors.Black);
        }

        /// <summary>
        /// Kui kasutaja vajutab loobu nupule, käivitatakse antud meetod ja siin suletakse aken.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        private void loobubutton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
